"""
@author: xiangping
@contact: xiangpingbu@gmail.com
@time: 2020/1/5 1:11 下午
@file: __init__
@Desc:
"""

import pickle

import pandas as pd


# rule_dict = pickle.load(open("HanFan_reason_dict.pk", "rb"))
# print(rule_dict.keys())
# for key in rule_dict['CG_LT_GL_GL04_CO2ZXFX']:
#     if 15000 in key:
#         p = rule_dict[key]

def load():
    m = pickle.load(open("lugang_activiness_short.pkl", "rb"))

    data = dict(CG_LT_GL_GL04_DWDB=180.22,CG_LT_GL_GL04_FYL=826.66,
             CG_LT_GL_GL04_TSWD=1475.0,CG_LT_GL_GL04_LFLL=4402.83,
             CG_LT_GL_GL04_RLB=517)

    columns = ['CG_LT_GL_GL04_FYL', 'CG_LT_GL_GL04_DWDB',
     'CG_LT_GL_GL04_LFLL', 'CG_LT_GL_GL04_RLB', 'CG_LT_GL_GL04_TSWD']
    d = {column:data[column] for column in columns}

    input_df = pd.DataFrame([d])

    predict = m.predict(input_df)
    print(predict)


def parse(condition_str):
    start = condition_str[0:1]
    condition_str = condition_str[1:]
    end = condition_str[-1]
    condition_str = condition_str[:-1]
    f1 = condition_str.split(",")[0]
    f2 = condition_str.split(",")[1]

    return start, end, f1, f2


def run():
    from interval3 import Interval
    import pandas as pd
    import pickle
    df = pd.read_excel("/Users/lifeng/Downloads/含钒-最佳值和操作范围0613.xls")

    rule_set = {}
    columns = list(df.columns)
    for index, row in df.iterrows():
        start, end, f1, f2 = parse(row["V2O5"])
        i = Interval(lower_bound=float(f1), upper_bound=float(f2),
                     lower_closed=start == "[",
                     upper_closed=end == "]")
        for column in columns:
            if column.startswith("CG"):
                print(column)
                inner_rule = rule_set.get(column,{})

                start, end, f1, f2 = parse(row[column])
                m = Interval(lower_bound=float(f1), upper_bound=float(f2),
                     lower_closed=start == "[",
                     upper_closed=end == "]")

                inner_rule[i] = m
                rule_set[column] = inner_rule
                pass


    pickle.dump(rule_set, open("./dist_dict.pk", "wb"))

load()